const express = require('express');
const { SimpleData, SuccessData } = require('../common/returnDataFormat');
const router = express.Router();
const adminController = require('../controller/adminController');
// 管理员修改用户信息
router.post('/modifyUser', async function (req, res) {
  let { user_id, ...userInfo } = req.body;
  const result = await adminController.modifyUser([user_id, userInfo]);
  res.send(result);
});

router.get('/selectUser', async (req, res) => {
  if (req.query.user_id) {
    const result = await adminController.selectUserById(req.query.user_id);
    res.send(SuccessData(result));
  } else {
    return res.send(SimpleData(500, '用户ID未传入'));
  }
});

//管理员封禁用户
router.get('/banUser', async function (req, res) {
  const result = await adminController.banUser(req.query.user_id);
  res.send(result);
});

//管理员删除用户
router.get('/delUser', async function (req, res) {
  console.log(req.query.user_id);
  const result = await adminController.delUser(req.query.user_id);
  res.send(result);
});

router.post('/addType', async (req, res) => {
  const name = req.body.name;
  const result = await adminController.addType(name);
  if (result.code == 200) {
    res.send(SimpleData(200, '添加分类成功'));
  } else {
    res.send(result);
  }
});
router.post('/modifyType', async (req, res) => {
  const result = await adminController.modifyType(req.body);
  if (result.code == 200) {
    res.send(SimpleData(200, '修改分类成功'));
  } else {
    res.send(result);
  }
});
router.get('/deleteType', async (req, res) => {
  const result = await adminController.deleteType(req.query);
  if (result.code == 200) {
    res.send(SimpleData(200, '删除分类成功'));
  } else {
    res.send(result);
  }
});
//添加管理员
//权限分配
module.exports = router;
